package com.toolbox.utils;

import org.apache.poi.xslf.usermodel.*;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class PPTGenerator {

    /**
     * 生成基本的演示文稿
     * @param title 演示文稿标题
     * @param slidesContent 幻灯片内容列表
     * @return 生成的PPT字节数组
     */
    public static byte[] generateBasicPresentation(String title, List<Map<String, String>> slidesContent) throws IOException {
        // 创建一个新的PPT文档
        XMLSlideShow ppt = new XMLSlideShow();
        
        try {
            // 设置页面大小
            Dimension pageSize = new Dimension(960, 540);
            ppt.setPageSize(pageSize);
            
            // 创建封面幻灯片
            XSLFSlide coverSlide = ppt.createSlide();
            createTitleSlide(coverSlide, title);
            
            // 创建内容幻灯片
            for (Map<String, String> slideContent : slidesContent) {
                XSLFSlide slide = ppt.createSlide();
                String slideTitle = slideContent.getOrDefault("title", "");
                String slideText = slideContent.getOrDefault("content", "");
                createContentSlide(slide, slideTitle, slideText);
            }
            
            // 将PPT文档写入字节数组
            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                ppt.write(out);
                return out.toByteArray();
            }
        } finally {
            ppt.close();
        }
    }

    /**
     * 生成标题幻灯片
     */
    private static void createTitleSlide(XSLFSlide slide, String title) {
        // 创建标题区域
        XSLFTextBox titleBox = slide.createTextBox();
        XSLFTextParagraph titlePara = titleBox.addNewTextParagraph();
        XSLFTextRun titleRun = titlePara.addNewTextRun();
        
        titleRun.setText(title);
        titleRun.setFontSize(44.0);
        titleRun.setFontColor(Color.BLACK);
        titleRun.setBold(true);
        
        // 设置标题框位置和大小
        Rectangle titleRect = new Rectangle(50, 100, 860, 150);
        titleBox.setAnchor(titleRect);
        
        // 添加副标题（可选）
        XSLFTextBox subtitleBox = slide.createTextBox();
        XSLFTextParagraph subtitlePara = subtitleBox.addNewTextParagraph();
        XSLFTextRun subtitleRun = subtitlePara.addNewTextRun();
        
        subtitleRun.setText("Generated by Toolbox Website");
        subtitleRun.setFontSize(24.0);
        subtitleRun.setFontColor(Color.GRAY);
        
        // 设置副标题框位置和大小
        Rectangle subtitleRect = new Rectangle(50, 250, 860, 50);
        subtitleBox.setAnchor(subtitleRect);
    }

    /**
     * 创建内容幻灯片
     */
    private static void createContentSlide(XSLFSlide slide, String title, String content) {
        // 创建标题区域
        XSLFTextBox titleBox = slide.createTextBox();
        XSLFTextParagraph titlePara = titleBox.addNewTextParagraph();
        XSLFTextRun titleRun = titlePara.addNewTextRun();
        
        titleRun.setText(title);
        titleRun.setFontSize(32.0);
        titleRun.setFontColor(Color.BLACK);
        titleRun.setBold(true);
        
        // 设置标题框位置和大小
        Rectangle titleRect = new Rectangle(50, 50, 860, 80);
        titleBox.setAnchor(titleRect);
        
        // 创建内容区域
        XSLFTextBox contentBox = slide.createTextBox();
        
        // 处理内容，按换行符分割段落
        String[] paragraphs = content.split("\n");
        for (String paragraph : paragraphs) {
            XSLFTextParagraph contentPara = contentBox.addNewTextParagraph();
            XSLFTextRun contentRun = contentPara.addNewTextRun();
            
            contentRun.setText(paragraph);
            contentRun.setFontSize(20.0);
            contentRun.setFontColor(Color.BLACK);
        }
        
        // 设置内容框位置和大小
        Rectangle contentRect = new Rectangle(50, 150, 860, 300);
        contentBox.setAnchor(contentRect);
    }

    /**
     * 根据模板生成PPT
     * @param templateType 模板类型
     * @param params 参数
     * @return 生成的PPT字节数组
     */
    public static byte[] generatePPTFromTemplate(String templateType, Map<String, Object> params) throws IOException {
        // 在实际项目中，这里应该从预定义的模板生成PPT
        // 这里简化实现，根据模板类型创建不同的演示文稿
        
        String title = (String) params.getOrDefault("title", "演示文稿");
        
        XMLSlideShow ppt = new XMLSlideShow();
        
        try {
            // 设置页面大小
            Dimension pageSize = new Dimension(960, 540);
            ppt.setPageSize(pageSize);
            
            // 创建封面幻灯片
            XSLFSlide coverSlide = ppt.createSlide();
            createTitleSlide(coverSlide, title);
            
            // 根据模板类型添加不同的内容幻灯片
            if ("business".equals(templateType)) {
                addBusinessSlides(ppt, params);
            } else if ("education".equals(templateType)) {
                addEducationSlides(ppt, params);
            } else if ("report".equals(templateType)) {
                addReportSlides(ppt, params);
            }
            
            // 将PPT文档写入字节数组
            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                ppt.write(out);
                return out.toByteArray();
            }
        } finally {
            ppt.close();
        }
    }

    /**
     * 添加商务演示幻灯片
     */
    private static void addBusinessSlides(XMLSlideShow ppt, Map<String, Object> params) {
        // 创建目录幻灯片
        XSLFSlide agendaSlide = ppt.createSlide();
        createContentSlide(agendaSlide, "目录", "1. 项目概述\n2. 市场分析\n3. 产品介绍\n4. 商业模式\n5. 财务规划\n6. 总结与展望");
        
        // 创建项目概述幻灯片
        if (params.containsKey("overview")) {
            String overview = (String) params.get("overview");
            XSLFSlide overviewSlide = ppt.createSlide();
            createContentSlide(overviewSlide, "项目概述", overview);
        }
        
        // 创建总结幻灯片
        XSLFSlide conclusionSlide = ppt.createSlide();
        createContentSlide(conclusionSlide, "总结与展望", "感谢您的聆听！");
    }

    /**
     * 添加教育演示幻灯片
     */
    private static void addEducationSlides(XMLSlideShow ppt, Map<String, Object> params) {
        // 创建目录幻灯片
        XSLFSlide agendaSlide = ppt.createSlide();
        createContentSlide(agendaSlide, "课程大纲", "1. 引言\n2. 核心概念\n3. 实践案例\n4. 总结\n5. 问答环节");
        
        // 创建核心概念幻灯片
        if (params.containsKey("concepts")) {
            String concepts = (String) params.get("concepts");
            XSLFSlide conceptsSlide = ppt.createSlide();
            createContentSlide(conceptsSlide, "核心概念", concepts);
        }
        
        // 创建总结幻灯片
        XSLFSlide conclusionSlide = ppt.createSlide();
        createContentSlide(conclusionSlide, "总结", "感谢您的学习！");
    }

    /**
     * 添加报告演示幻灯片
     */
    private static void addReportSlides(XMLSlideShow ppt, Map<String, Object> params) {
        // 创建目录幻灯片
        XSLFSlide agendaSlide = ppt.createSlide();
        createContentSlide(agendaSlide, "报告内容", "1. 背景介绍\n2. 数据统计\n3. 分析结果\n4. 建议与措施\n5. 结论");
        
        // 创建分析结果幻灯片
        if (params.containsKey("analysis")) {
            String analysis = (String) params.get("analysis");
            XSLFSlide analysisSlide = ppt.createSlide();
            createContentSlide(analysisSlide, "分析结果", analysis);
        }
        
        // 创建结论幻灯片
        XSLFSlide conclusionSlide = ppt.createSlide();
        createContentSlide(conclusionSlide, "结论", "以上是本次报告的全部内容。");
    }
}